//
//  KSPullView.swift
//  ZeroShare
//
//  Created by July on 2020/3/22.
//  Copyright © 2020 saeipi. All rights reserved.
//

import UIKit

class KSPullView: UIView {
    private var collectionView: UICollectionView!
    var strokeColor: UIColor?
    var fillColor: UIColor?
    var kitRadius: CGFloat        = 5.0
    var lineWidth: CGFloat        = 1.0
    var triangleCenterX: CGFloat = 0.0
    var isTop: Bool              = true
    var timeStr = "5"{
        willSet{
            self.collectionView.reloadData()
        }
    }
    var timeBlock:((String,String)->Void)?
    
    lazy var timeArray:[(String,String,Int)] = {
        let timeArray = [("分时","kineLine",60),
                        ("1m","1",60),
                        ("5m","5",60*5),
                        ("15m","15",60*15),
                        ("30m","30",60*30),
                        ("1h","60",60*60),
                        ("2h","120",60*120),
                        ("4h","240",60*240),
                        ("6h","360",60*360),
                        ("12h","720",60*720),
                        ("1d","1440",60*1440),
                        ("1w","10080",60*10080)]
        return timeArray
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createUI(){
        let flowLayout                     = UICollectionViewFlowLayout.init()
        flowLayout.itemSize                = CGSize(width: (self.frame.size.width - 32)/5, height: 40)
        flowLayout.minimumLineSpacing      = 0//同一组当中，行与行之间的最小行间距，但是不同组之间的不同行cell不受这个值影响。
        flowLayout.minimumInteritemSpacing = 0//同一行的cell中互相之间的最小间隔，设置这个值之后，那么cell与cell之间至少为这个值
        flowLayout.scrollDirection         = .vertical

        collectionView                     = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.delegate            = self
        collectionView.dataSource          = self
        collectionView.backgroundColor     = UIColor.clear
        collectionView.isScrollEnabled     = false
        self.addSubview(collectionView)

        collectionView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview().offset(9)
            make.bottom.equalToSuperview().offset(-4)
        }
        collectionView.register(KSChartMenuCell.self, forCellWithReuseIdentifier: "timeCell")
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    func drawBubble() {
        let kitWidth:CGFloat          = self.frame.width
        let kitHeight:CGFloat         = self.frame.height
        let angleBottomWidth: CGFloat = 12.0
        let triangleHeight: CGFloat   = 5
        
        let triangleBottomY           = isTop ? (lineWidth + triangleHeight) : (kitHeight - lineWidth - triangleHeight)
        let cornerY                   = isTop ? lineWidth : (kitHeight - lineWidth)

        let leftTopX                  = kitRadius + lineWidth
        let leftTopY                  = isTop ? triangleBottomY : lineWidth

        let leftTopArcX               = leftTopX
        let leftTopArcY               = leftTopY + kitRadius

        let rightTopArcX              = kitWidth - lineWidth - kitRadius
        let rightTopArcY              = leftTopArcY

        let rightBottomArcX           = rightTopArcX
        let rightBottomArcY           = isTop ? (kitHeight - kitRadius) : (kitHeight - lineWidth - triangleHeight - kitRadius)

        let leftBottomArcY            = rightBottomArcY
        let leftBottomArcX            = leftTopX

        let bezierPath                = UIBezierPath.init()
        //起始点
        bezierPath.move(to: CGPoint(x: leftTopX, y: leftTopY))
        //顶部三角
        if isTop {
            bezierPath.addLine(to: CGPoint(x: 20 - angleBottomWidth / 2, y: triangleBottomY))
            bezierPath.addLine(to: CGPoint(x: 20, y: cornerY))
            bezierPath.addLine(to: CGPoint(x: 20 + angleBottomWidth / 2, y: triangleBottomY))
        }
        //右上角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: rightTopArcX, y: rightTopArcY), radius: kitRadius, startAngle: -CGFloat(Double.pi / 2), endAngle: 0.0, clockwise: true)
        //右下角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: rightBottomArcX, y: rightBottomArcY), radius: kitRadius, startAngle: 0.0, endAngle: CGFloat(Double.pi / 2), clockwise: true)
        if isTop == false {
            bezierPath.addLine(to: CGPoint(x: triangleCenterX + angleBottomWidth / 2, y: triangleBottomY))
            bezierPath.addLine(to: CGPoint(x: triangleCenterX, y: cornerY))
            bezierPath.addLine(to: CGPoint(x: triangleCenterX - angleBottomWidth / 2, y: triangleBottomY))
        }
        //左下角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: leftBottomArcX, y: leftBottomArcY), radius: kitRadius, startAngle: CGFloat(Double.pi / 2), endAngle: CGFloat(Double.pi), clockwise: true)
        //左上角圆弧
        bezierPath.addArc(withCenter: CGPoint(x: leftTopArcX, y: leftTopArcY), radius: kitRadius, startAngle: CGFloat(Double.pi), endAngle: CGFloat(Double.pi * 1.5), clockwise: true)

        let maskLayer                 = CAShapeLayer()
        maskLayer.frame               = CGRect(x: 0, y: 0, width: kitWidth, height: kitHeight)

        let borderLayer               = CAShapeLayer()
        borderLayer.frame             = CGRect(x: 0, y: 0, width: kitWidth, height: kitHeight)
        borderLayer.lineWidth         = lineWidth
        borderLayer.strokeColor       = strokeColor?.cgColor
        borderLayer.fillColor         = fillColor?.cgColor

        maskLayer.path                = bezierPath.cgPath
        borderLayer.path              = bezierPath.cgPath

        self.layer.insertSublayer(borderLayer, at: 0)
        self.layer.mask              = maskLayer
    }

}

extension KSPullView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.timeArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let data = self.timeArray[indexPath.row]
        let cell:KSChartMenuCell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! KSChartMenuCell
        if data.1 == self.timeStr {
            cell.textLabel.textColor = UIColor.yellow
        }else{
            cell.textLabel.textColor = UIColor.black
        }
        cell.textLabel.text = data.0
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let data = self.timeArray[indexPath.row]
        if self.timeBlock != nil  {
            self.timeBlock!(data.1,data.0)
        }
    }
}
